package indi.zhifa.study2024.busy.generaltest.business.helloworld.service.impl;

import indi.zhifa.study2024.busy.generaltest.business.helloworld.entity.response.ServerInfo;
import indi.zhifa.study2024.busy.generaltest.business.helloworld.entity.response.SiteInfo;
import indi.zhifa.study2024.busy.generaltest.business.helloworld.service.IHelloWorldService;
import indi.zhifa.study2024.busy.generaltest.enums.common.EWeekDay;
import indi.zhifa.study2024.busy.generaltest.enums.person.EGender;
import org.springframework.stereotype.Service;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.time.LocalDateTime;

@Service
public class HelloWorldServiceImpl implements IHelloWorldService {
    @Override
    public String helloWorld() {
        return "hello world";
    }

    @Override
    public SiteInfo siteInfo() {
        SiteInfo siteInfo = new SiteInfo();
        siteInfo.setName("芝法酱后端开发小课堂");
        siteInfo.setCompany("芝法酱氪姬有爱公司");
        siteInfo.setUrl("www.gomapesuto.com");
        siteInfo.setDesc("这是一个有爱的技术学习网站");
        LocalDateTime currentTime = LocalDateTime.now();
        siteInfo.setCurDate(currentTime.toLocalDate());
        siteInfo.setCurTime(currentTime.toLocalTime());
        return siteInfo;
    }

    @Override
    public ServerInfo serverInfo() {
        ServerInfo serverInfo = new ServerInfo();
        OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
        int availableProcess = osBean.getAvailableProcessors();
        serverInfo.setAvailableProcess(availableProcess);
        if (osBean instanceof com.sun.management.OperatingSystemMXBean) {
            com.sun.management.OperatingSystemMXBean sunOsBean = (com.sun.management.OperatingSystemMXBean) osBean;
            // 获取系统总物理内存 (bytes)
            long totalPhysicalMemory = sunOsBean.getTotalPhysicalMemorySize();
            // 获取系统空闲物理内存 (bytes)
            long freePhysicalMemory = sunOsBean.getFreePhysicalMemorySize();
            // 获取系统CPU负载 (0.0 - 1.0)
            double systemCpuLoad = sunOsBean.getSystemCpuLoad();
            serverInfo.setTotalPhysicalMemory(totalPhysicalMemory);
            serverInfo.setFreePhysicalMemory(freePhysicalMemory);
            serverInfo.setSystemCpuLoad(systemCpuLoad);
        }
        return serverInfo;
    }

    @Override
    public String helloGender(EGender pGender) {
        return "hello "+pGender.getDesc();
    }

    @Override
    public String helloWeekday(EWeekDay pWeekDay) {
        return "hello "+pWeekDay.getDesc();
    }
}
